package top.zywork.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zywork.common.DateParseUtils;
import top.zywork.common.RegexUtils;
import top.zywork.enums.DatePatternEnum;
import top.zywork.enums.GoodsOrderStatusEnum;
import top.zywork.query.GoodsOrderQuery;
import top.zywork.service.MallStatisticsService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.StatisticsDayCountVO;
import top.zywork.vo.echarts.BasicLineChartVO;
import top.zywork.vo.echarts.MultipleLineChartVO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 商城相关统计控制器类<br/>
 *
 * 创建于2019-11-16<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/mall-stat")
public class MallStatisticsController extends BaseController {

    private MallStatisticsService mallStatisticsService;

    /**
     * 统计总商品数，总上架商品数，总SKU数，总订单数，总待付款订单数，总待发货订单数
     * @return
     */
    @PostMapping("goods-order-count")
    public ResponseStatusVO countGoodsAndOrder() {
        Long totalGoods = mallStatisticsService.countAllGoods();
        Long totalOnShelfGoods = mallStatisticsService.countAllOnShelfGoods();
        Long totalSkus = mallStatisticsService.countAllGoodsSkus();
        Long totalOrders = mallStatisticsService.countAllGoodsOrder();
        Long totalWaitPaymentOrders = mallStatisticsService.countAllGoodsOrderByStatus(GoodsOrderStatusEnum.WAIT_PAYMENT.getValue().byteValue());
        Long totalWaitDeliveryOrders = mallStatisticsService.countAllGoodsOrderByStatus(GoodsOrderStatusEnum.WAIT_DELIVER.getValue().byteValue());
        return ResponseStatusVO.ok("统计总商品数，总上架商品数，总SKU数，总订单数，总待付款订单数，总待发货订单数",
                new Object[]{totalGoods, totalOnShelfGoods, totalSkus, totalOrders, totalWaitPaymentOrders, totalWaitDeliveryOrders});
    }

    /**
     * 指定时间周期内的总商品数
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("all-goods-count-by-date")
    public ResponseStatusVO countAllGoodsByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        Long totalCount = mallStatisticsService.countAllGoodsByTime(beginCalendar.getTime(), endCalendar.getTime());
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "总商品数", totalCount);
    }

    /**
     * 指定时间周期内的每日商品数（用于echarts统计）
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("goods-count-by-date")
    public ResponseStatusVO countGoodsByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        List<StatisticsDayCountVO> statisticsDayCountVOList = mallStatisticsService.countDayGoodsByTime(beginCalendar.getTime(), endCalendar.getTime());
        BasicLineChartVO basicLineChartVO = new BasicLineChartVO();
        List<String> xAxisData = new ArrayList<>();
        List<Number> seriesData = new ArrayList<>();
        for (StatisticsDayCountVO statisticsDayCountVO : statisticsDayCountVOList) {
            xAxisData.add(statisticsDayCountVO.getDateStr());
            seriesData.add(statisticsDayCountVO.getTotalLongCount());
        }
        basicLineChartVO.setxAxisData(xAxisData);
        basicLineChartVO.setSeriesData(seriesData);
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "的每日商品数", basicLineChartVO);
    }

    /**
     * 获取指定周期内的总订单数
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("all-goods-order")
    public ResponseStatusVO getAllGoodsOrderByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        Long totalCount = mallStatisticsService.countAllGoodsOrderByTime(beginCalendar.getTime(), endCalendar.getTime());
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "总订单数", totalCount);
    }

    /**
     * 获取指定周期内的每日订单数（用于echarts统计）
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("goods-order-by-date")
    public ResponseStatusVO getGoodsOrderByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        List<StatisticsDayCountVO> statisticsDayCountVOList = mallStatisticsService.countDayGoodsOrderByTime(beginCalendar.getTime(), endCalendar.getTime(),null);
        BasicLineChartVO basicLineChartVO = new BasicLineChartVO();
        List<String> xAxisData = new ArrayList<>();
        List<Number> seriesData = new ArrayList<>();
        for (StatisticsDayCountVO statisticsDayCountVO : statisticsDayCountVOList) {
            xAxisData.add(statisticsDayCountVO.getDateStr());
            seriesData.add(statisticsDayCountVO.getTotalLongCount());
        }
        basicLineChartVO.setxAxisData(xAxisData);
        basicLineChartVO.setSeriesData(seriesData);
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "的每日订单数", basicLineChartVO);
    }

    /**
     * 获取指定周期内的各个状态每日订单数（用于订单echarts统计）
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("goods-order-by-date-status")
    public ResponseStatusVO getGoodsOrderStatusByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        //待付款
        GoodsOrderQuery orderQuery = GoodsOrderQuery.builder().orderStatus(GoodsOrderStatusEnum.WAIT_PAYMENT.getValue().byteValue()).build();
        List<StatisticsDayCountVO> waitPayDayCountVOList = mallStatisticsService.countDayGoodsOrderByTime(beginCalendar.getTime(), endCalendar.getTime(),orderQuery);
        //已支付
       orderQuery.setOrderStatus(GoodsOrderStatusEnum.PAY_SUCCESS.getValue().byteValue());
        List<StatisticsDayCountVO> paySuccessDayCountVOList = mallStatisticsService.countDayGoodsOrderByTime(beginCalendar.getTime(), endCalendar.getTime(),orderQuery);
        //待发货
        orderQuery.setOrderStatus(GoodsOrderStatusEnum.WAIT_DELIVER.getValue().byteValue());
        List<StatisticsDayCountVO> waitDeliverDayCountVOList = mallStatisticsService.countDayGoodsOrderByTime(beginCalendar.getTime(), endCalendar.getTime(),orderQuery);
        MultipleLineChartVO multipleLineChartVO = new MultipleLineChartVO();
        List<String> xAxisData = new ArrayList<>();
        List<Number> waitPayDataList = new ArrayList<>();
        List<Number> paySuccessDataList = new ArrayList<>();
        List<Number> waitDeliverDataList = new ArrayList<>();
        Long waitPayTotal = 0L;
        Long paySuccessTotal= 0L;
        Long waitDeliverTotal = 0L;
        for (int i = 0; i <waitPayDayCountVOList.size() ; i++) {
            xAxisData.add(waitPayDayCountVOList.get(i).getDateStr());
            //待付款
            waitPayDataList.add(waitPayDayCountVOList.get(i).getTotalLongCount());
            waitPayTotal += waitPayDayCountVOList.get(i).getTotalLongCount();
            //已支付
            paySuccessDataList.add(paySuccessDayCountVOList.get(i).getTotalLongCount());
            paySuccessTotal += paySuccessDayCountVOList.get(i).getTotalLongCount();
            //待发货
            waitDeliverDataList.add(waitDeliverDayCountVOList.get(i).getTotalLongCount());
            waitDeliverTotal += waitDeliverDayCountVOList.get(i).getTotalLongCount();
        }
        List<List<Number>> seriesDataArray = new ArrayList<>();
        seriesDataArray.add(waitPayDataList);
        seriesDataArray.add(paySuccessDataList);
        seriesDataArray.add(waitDeliverDataList);
        List<String> legend = new ArrayList<>();
        legend.add("待付款");
        legend.add("已支付");
        legend.add("待发货");
        multipleLineChartVO.setLegend(legend);
        multipleLineChartVO.setxAxisData(xAxisData);
        multipleLineChartVO.setSeriesDataArray(seriesDataArray);
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "的每日订单统计", new Object[]{multipleLineChartVO,waitPayTotal,paySuccessTotal,waitDeliverTotal});
    }

    @Autowired
    public void setMallStatisticsService(MallStatisticsService mallStatisticsService) {
        this.mallStatisticsService = mallStatisticsService;
    }
}
